package com.example.jpushdemo;

import cn.jpush.android.api.JPushInterface;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.TextView;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

public class TestActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TextView tv = new TextView(this);
        tv.setText("用户自定义打开的Activity");
        Intent intent = getIntent();
       /* if (null != intent) {
            Bundle bundle = getIntent().getExtras();
            // 通过JpushInterface 可以获取所有的字段
	        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
	        String content = bundle.getString(JPushInterface.EXTRA_ALERT);
	        tv.setText("Title : " + title + "  " + "Content : " + content);
            // 获取附加字段, 字符串的格式json
            String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (json != null) {
                // 后台设置的字段
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    String url = jsonObject.getString("url");
                    Toast.makeText(this, url, Toast.LENGTH_SHORT).show();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }*/
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
            if (json != null) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    String url = jsonObject.getString("url");
                    mWebView = new WebView(this);
                    mWebView.loadUrl(url);
                    tv.setText("Title : ");
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        addContentView(mWebView, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        addContentView(tv, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}
